using System;
using System.Collections.Generic;
using System.Html;
using jQueryApi;

namespace Yoshi.Classes
{
    public partial class AbstractView
    {
        private const string DownMark = "&#x25BC;";
        //private const string LeftMark = "&#x25C4;";
        //private const string RightMark = "&#x25BA;";

        private List<string> _dropMenuBaseList;

        protected void AddDropMenu(jQueryObject el, IEnumerable<KeyValuePair<string, object>> lists, ControlEventDelegate handler)
        {
            if (!Script.IsValue(lists)) return;

            // Convert to List<> and save initial for sorting
            _dropMenuBaseList = new List<string>();
            foreach (KeyValuePair<string,object> p in lists)
            {
                _dropMenuBaseList.Add(p.Key);
            }
            List<string> names = _dropMenuBaseList.Clone();
            names.Reverse();

            jQueryObject list;
            jQueryObject widget;
            jQueryObject priorList = jQuery.Select(".DropMenu", el.Parent());
            if (priorList.Length == 0)
            {

                widget = jQuery.FromHtml("<div class='DropMenuWidget'/>");
                // The list fire drop down button
                jQueryObject fire = jQuery.FromHtml("<span class='DropMenuFire'/>")
                    .AppendTo(widget)
                    .Html(DownMark)
                    .Height(el.GetOuterHeight())
                    ;
                // Element hiddin behind list used to capture focus outine and hide it
                jQueryObject focs = jQuery.FromHtml("<span class='DropMenuFocus' style='color:transparent;background:transparent;position:absolute;top:20px;right:10px' tabindex='999'/>")
                    .AppendTo(widget)
                    .Html(DownMark)
                    ;
                list = jQuery.FromHtml("<ul class='DropMenu'/>")
                    .AppendTo(widget)
                    ;

                widget.InsertAfter(el)
                    ;
                fire
                    .Click(
                        delegate(jQueryEvent e)
                        {
                            e.PreventDefault();
                            if (!list.Is(":visible"))
                            {
                                focs.Focus();
                                list.Show();
                            }
                        }
                    )
                    ;
                focs
                    .FocusOut(
                        delegate
                        {
                            Window.SetTimeout(
                                delegate
                                {
                                    fire.Focus();
                                    list.FadeOut(120);
                                }, 1);
                        }
                    )
                    ;

                Unselectable(fire);
                Unselectable(focs);
            }
            else
            {
                widget = jQuery.Select(".DropMenuWidget", el.Parent());
                list = priorList;
                list.Empty();
            }

            foreach (string n in names)
            {
                jQueryObject item = jQuery.FromHtml("<li class='DropMenuItem'/>")
                    .AppendTo(list)
                    .Data("MenuData", n)
                    .Click(
                        delegate(jQueryEvent e)
                        {
                            // Click could be on <a> or <li> hence use CurrentTarget to get <li>
                            string nd = (string) jQuery.FromElement(e.CurrentTarget).GetDataValue("MenuData");
                            handler(e, nd);
                        }
                    )
                    ;
                jQuery.FromHtml("<a/>")
                    .AppendTo(item)
                    .Html(n)
                    .Attribute("href", "javascript:;")
                    ;
            }

            list.CSS("top", (jQuery.Select(".DropMenuFire", widget).GetHeight() + 1) + "px");
        }

        private static void Unselectable(jQueryObject el)
        {
            Element elem = el[0];
            Type.SetField(elem, "onselectstart", Script.Literal("function() { return false; }"));
            Type.SetField(elem.Style, "MozUserSelect", "none");
            Type.SetField(elem.Style, "KhtmlUserSelect", "none");
            Type.SetField(elem, "unselectable", "on");
        }
    }
}